package cn.jsj.machine.controller;

import cn.jsj.machine.exceptions.MyRuntimeIdentityException;
import cn.jsj.machine.exceptions.MyRuntimeParamsException;
import cn.jsj.machine.pojo.CMachine;
import cn.jsj.machine.pojo.CPart;
import cn.jsj.machine.service.MachineService;
import cn.jsj.machine.tool.HashMapBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@Validated
public class MachineController {

    @Autowired
    private MachineService machineService;

    @Autowired
    private HashMapBuilder hashMapBuilder;

    @ResponseBody
    @RequestMapping(value = "getAllMachine", method = {RequestMethod.POST}, params = {"account","password"})
    public Map<String,Object> getAllMachineInformation(@RequestParam("account") Integer account,@RequestParam("password") String password){
        Map<String, Object> result  = null;
        try {
            List<CMachine> storage = this.machineService.getMachineList(account, password);
            result = this.hashMapBuilder.buildList(storage);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.buildList(null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }


    @ResponseBody
    @PostMapping(value = "createNewProduct", params = {"paramName", "account","password"})
    public Map<String, Object> createMachine(@RequestParam("paramName") String paramName, @RequestParam("account") Integer account,@RequestParam("password") String password) {
        this.machineService.addNewProduct(account, password, paramName);
        Map<String, Object> result  = new HashMap<>();
        result.put("message", "添加成功");
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "addOneMachine", method = {RequestMethod.POST})
    public Map<String,Object> addOneMachine(@RequestBody Map<String,Object> param){
        Map<String,Object> result = null;
        try {
            String machineName = (String)param.get("name");
            Integer account = (Integer)param.get("account");
            String password = (String)param.get("password");
            String ts = ((String)param.get("infor"));
            if(ts == null || ts.length() == 0){
                result = this.hashMapBuilder.build(0,"请添加产品原料类型",null);
                return result;
            }
            Map<Integer,Integer> partInf = new HashMap<>();
            String[] tslist = ts.trim().split(" ");
            for(int i = 1;i<tslist.length;i+=2){
                partInf.put(Integer.parseInt(tslist[i-1]),Integer.parseInt(tslist[i]));
            }
            Integer js = this.machineService.insertOneMachine(account,password,machineName,partInf);
            if(js == 1){
                result = this.hashMapBuilder.build(1,"产品信息添加成功，包含"+partInf.size()+"种原料信息。",null);
            }else{
                result = this.hashMapBuilder.build(1,"存在该产品，原料信息添加完成",null);
            }
        }catch (ConstraintViolationException | MyRuntimeParamsException | MyRuntimeIdentityException | NullPointerException | NumberFormatException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "getPartListOfMachine", method = {RequestMethod.POST}, params = {"account","password","machineID"})
    public Map<String,Object> getPartListOfMachine(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID){
        Map<String,Object> result =null;
        try {
            if(machineID == 0){
                List<CPart> st = new LinkedList<>();
                result = this.hashMapBuilder.buildList(st);
            }else {
                List<CPart> storage = this.machineService.getOneMachineParts(account, password, machineID);
                result = this.hashMapBuilder.buildList(storage);
            }
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.buildList(null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "productMachine", method = {RequestMethod.POST},params = {"account","password","machineID","cutNumber"})
    public Map<String,Object> productNewMachine(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID,@RequestParam("cutNumber") Integer cutNumber){
        Map<String,Object> result = null;
        try {
            this.machineService.productOneMachine(account, password, machineID, cutNumber);
            result = this.hashMapBuilder.build(1,"库存信息修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "deleteMachine", method = {RequestMethod.POST}, params = {"account","password","machineID"})
    public Map<String,Object> deleteOneMachhine(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID){
        Map<String,Object> result = null;
        try {
            this.machineService.deleteMachine(account, password, machineID);
            result = this.hashMapBuilder.build(1,"产品信息删除成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "machineReName", method = {RequestMethod.POST},params = {"account","password","machineID","newName"})
    public Map<String,Object> rename(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID,@RequestParam("newName") String newName){
        Map<String,Object> result = null;
        try {
            this.machineService.machineReName(account, password, machineID, newName);
            result = this.hashMapBuilder.build(1,"产品名称修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "addOnePartToMachineByPartName", method = {RequestMethod.POST}, params = {"account","password","machineID","partName","needNumber"})
    public Map<String, Object> addOnePartToMachine(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID,@RequestParam("partName") String partName,@RequestParam("needNumber") Integer needNumber){
        Map<String,Object> result = null;
        try {
            this.machineService.addOnePartToMachineWithPartName(account,password,machineID,partName,needNumber);
            result = this.hashMapBuilder.build(1,"添加成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "deleteOnePartFromMachine", method = {RequestMethod.POST}, params = {"account","password","machineID","partID"})
    public Map<String,Object> deleteOnePartFromMachine(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID,@RequestParam("partID") Integer partID){
        Map<String,Object> result = null;
        try {
            this.machineService.deleteOnePartFromMachine(account, password, machineID, partID);
            result = this.hashMapBuilder.build(1,"删除成功", null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "changePartNeedNumber", method = {RequestMethod.POST}, params = {"account","password","machineID","partID","needNum"})
    public Map<String,Object> changePartNeedNumber(@RequestParam("account") Integer account, @RequestParam("password") String password, @RequestParam("machineID") Integer machineID, @RequestParam("partID") Integer partID,@RequestParam("needNum") Integer needNum){
        Map<String,Object> result = null;
        try {
            this.machineService.changePartNeedNumber(account, password, machineID, partID, needNum);
            result = this.hashMapBuilder.build(1,"修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "descriptionReset", method = {RequestMethod.POST}, params = {"account","password","machineID","newStr"})
    public Map<String,Object> resetDescription(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("machineID") Integer machineID,@RequestParam("newStr") String newStr){
        Map<String,Object> result = null;
        try {
            this.machineService.resetMachineDescription(account, password, machineID, newStr);
            result = this.hashMapBuilder.build(1,"修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }
}
